#include "videothread.h"
#include <string.h>
videothread::videothread(QWidget *parent) : QWidget(parent)
{
    qDebug() << "gouzao hanshu ";
    pixmap=new QPixmap();
    this->socket=NULL;

    picbuf = (char *)malloc(1024*1024);

}


videothread::~videothread()
{
    free(picbuf);
}
void videothread::run()
{
    qDebug() << "run";
    if(this->socket == NULL)
    {
        qDebug() << "run socket is null";
        this->socket = new QTcpSocket();
        this->socket->connectToHost("192.168.0.250",58889);
        //发送信号者为socket这个实例，发送的信号是connected(),接收者为Form,槽函数为msconnected()
        connect(this->socket,SIGNAL(connected()),this,SLOT(msconnected()));
//        connect(this->socket,SIGNAL(disconnected()),this,SLOT(msdisconnected()));
    }
    else
    {
        qDebug() << "run socket isn't null";
        timer_movie->start(100);
    }
}

void videothread::msconnected()
{
    qDebug() << "connected";
    timer_movie = new QTimer(this);
    connect(timer_movie,SIGNAL(timeout()),this,SLOT(updatepic()));
    timer_movie->start(100);
}
void videothread::msdisconnected()
{
    qDebug() << "msdisconnected";
    if(timer_movie->isActive())
        timer_movie->stop();
    this->socket->close();
    this->socket = NULL;
}

void videothread::updatepic()
{
    qDebug() << "updatepic";
    unsigned int ret;
    char *request = "pic";      //请求命令
    char response[20];          //服务器发给QT的回复
    char *len;                  //
    unsigned int piclen;        //图片长度
   // char picbuf[1024 * 1024];   //存放图片
    ret = this->socket->write(request, strlen(request));  //发送请求图片命令
    if (ret != strlen(request)) {
        qDebug() << "send request failed";
        timer_movie->stop();
        this->socket->close();
    }

    this->socket->flush();    //刷新socket
    this->socket->waitForReadyRead(30000);

    memset(response, 0, sizeof(response));
    ret = this->socket->read(response, sizeof(response));
    if (ret != sizeof(response)) {
        qDebug() << "recv response failed";
        timer_movie->stop();
        this->socket->close();
        this->socket = NULL;
    }

    len = strstr(response, "len");
    if (len == NULL) {
        qDebug() << "response header is error";
        timer_movie->stop();
        this->socket->close();
        this->socket = NULL;
    }

    *len = '\0';
    //从response中读取图片长度
    piclen = atoi(response);

    qDebug() << "piclen: " << piclen;

    unsigned int total = 0;
    //循环读取pic信息
    while (total < piclen) {
        ret = this->socket->read(picbuf+total, piclen-total);
        if (ret < 0) {
            qDebug() << "recv pic failed" << ret;
            timer_movie->stop();
            this->socket->close();
            this->socket = NULL;
            return;
        } else if (ret == 0) {
            this->socket->waitForReadyRead(30000);
            continue;
        } else
            total += ret;

        qDebug() << "total: " << total;
    }
    pixmap->loadFromData((const uchar *)picbuf, piclen, "JPEG");
    qDebug() << "pixmap shezhi";
    emit display_movie_signals();
}
